package com.aaa.controller.order;

import com.aaa.entity.Goods;
import com.aaa.entity.Order;
import com.aaa.entity.ResponseDto;
import com.aaa.service.GoodsService;
import com.aaa.service.OrderService;
import com.aaa.service.impl.GoodsServiceImpl;
import com.aaa.service.impl.OrderServiceImpl;
import com.aaa.util.BusinessException;
import com.aaa.util.IntegerUtils;
import com.google.gson.Gson;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/ConsumerGoodsListServlet1")
public class ConsumerGoodsListServlet extends HttpServlet {
    GoodsService goodsService;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json");
        Integer pageNumber = IntegerUtils.ToInteger(req.getParameter("pageNumber"));
        Integer pageSize = IntegerUtils.ToInteger(req.getParameter("pageSize"));
        String searchGoodsId = req.getParameter("searchGoodsId");
        String searchGoodsName = req.getParameter("searchGoodsName");
        
        Map<String, Object> map=new HashMap<>();
        try {
            map = goodsService.consumerGoodList(pageNumber,pageSize, searchGoodsId, searchGoodsName);
        } catch (BusinessException e) {
            e.printStackTrace();
        }
        //获取总数
        int count=goodsService.getConsumerGoodsInfoCount(searchGoodsId,searchGoodsName);
        System.out.println("总数:"+count);
        ResponseDto responseDto = new ResponseDto();
        if (map != null){
            responseDto.setStatus(ResponseDto.SUCCESS_CODE);
            responseDto.setCount(count);
            responseDto.setData(map);
            responseDto.setMessage("操作成功");
        }else {
            responseDto.setStatus(ResponseDto.FAILURE_CODE);
            responseDto.setMessage("操作失败");
        }
        resp.getWriter().write(new Gson().toJson(responseDto));

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req, resp);
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        goodsService = new GoodsServiceImpl();
    }
}
